package com.sptus.service.impl;

import com.alibaba.fastjson2.JSON;
import com.sptus.mqtt.DataLoaderConfig;
import com.sptus.service.IEmailService;
import com.sptus.system.utils.GmailSendVerificationCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: create by Kim
 * @description: com.sptus.service.impl
 * @date:2025-08-05
 */
@Service
public class EmailServiceImpl implements IEmailService {

    private static final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);

    @Override
    @Async("asyncExecutor")
    public void sendDeviceWarningInfoEmail(List<String> targetEmailList, String traceId, String sn, String msg, String content) {
        logger.info("开始异步发送邮件，线程：{}，收件人：{}，SN：{}，MSG：{}", Thread.currentThread().getName(), JSON.toJSONString(targetEmailList), sn, msg);
        for (String targetEmail : targetEmailList) {
            try {
                GmailSendVerificationCode.sendDeviceWarningInfoEmail(targetEmail, traceId, sn, msg, content, DataLoaderConfig.getGlobalData());
                logger.info("邮件发送完成，收件人：{}，SN：{}，MSG：{}", targetEmail, sn, msg);
            } catch (Exception e) {
                logger.error("邮件发送失败，收件人：{}，SN：{}，MSG：{}，错误：{}", targetEmail, sn, msg, e.getMessage(), e);
            }
        }
    }
}